from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
import os
from launch_ros.parameter_descriptions import ParameterValue
from launch.substitutions import Command, LaunchConfiguration
from launch.actions import DeclareLaunchArgument
from launch.conditions import IfCondition

def generate_launch_description():
    go2_description_pkg = get_package_share_directory("go2_description")
    
    use_joint_state_publisher = DeclareLaunchArgument(
        name="use_joint_state_publisher",
        default_value="false"
    )
    
    robot_desc = ParameterValue(Command(["xacro ", os.path.join(go2_description_pkg, "urdf", "go2_description.urdf")]))
    
    robot_state_publisher = Node(
        package="robot_state_publisher",
        executable="robot_state_publisher",
        parameters=[{"robot_description": robot_desc}]  # 修复拼写：paramters → parameters
    )
    
    joint_state_publisher = Node(
        package="joint_state_publisher",
        executable="joint_state_publisher",
        condition=IfCondition(LaunchConfiguration("use_joint_state_publisher"))
    )
    
    return LaunchDescription([
        use_joint_state_publisher,
        robot_state_publisher,
        joint_state_publisher
    ])
